<?php

namespace App\Controller\Home;

use App\Controller\Example\OpenPrizeExample;
use App\Controller\Example\UserExample;
use App\Controller\Example\WinningExample;
use App\Models\ConfigModel;
use App\Models\Conn;
use App\Models\OpenPrizeModel;
use App\Models\PoolModel;
use App\Models\UsersModel;
use App\Models\WinningModel;
use Config\Config;
use Core\Common\Common;
use Core\Enums\DigitEnum;
use Core\Enums\ErrorEnum;
use Core\Enums\PrizeRuleEnum;
use Core\Result\Result;
use Core\Utils\Utils;

class Winning
{
    /**
     * TODO 抽奖信息
     * entry
     * return json
     * */
    public function winningInfo($data){
        $assemble = Common::assemble(new WinningExample(), $data);
        $conn = new Conn();
        $userInside = UsersModel::getUserInside($conn, $assemble->token);
        $winning["jackpot"] = PoolModel::getPoolInside($conn)["devote"];
        $winning["newPrize"] = OpenPrizeModel::getOpenPrizeInside($conn);
        $winning["currentNumber"] = WinningModel::getWinningNewCy($conn,$winning["newPrize"]["sn"],$userInside["id"])["number"];
        if(empty($winning["currentNumber"])){
            $winning["is_JoinWinning"] = false;
        }else{
            $winning["is_JoinWinning"] = true;
        }
        $lastOpenPrizeInside = OpenPrizeModel::getLastOpenPrizeInside($conn);
        $winning["lastPrize"] = $lastOpenPrizeInside;
        $winningInside = WinningModel::getWinningInside($conn, $lastOpenPrizeInside["sn"]);
        $level1 = 1;$level2 = 2;
        $winningCountInside1 = WinningModel::getWinningCountInside($conn, $lastOpenPrizeInside["sn"], $level1);
        $winningCountInside2 = WinningModel::getWinningCountInside($conn, $lastOpenPrizeInside["sn"], $level2);
        $winningCountInside1["winning"] = round($winningCountInside1["winning"],0).'';
        $winningCountInside1["level"] = $level1.'';
        $winningCountInside1["sn"] = $winning["newPrize"]["sn"].'';
        $winningCountInside1["number"] = $lastOpenPrizeInside["number"].'';
        $winningCountInside2["winning"] = round($winningCountInside2["winning"],0).'';
        $winningCountInside2["level"] =$level2.'';
        $winningCountInside2["sn"] = $winning["newPrize"]["sn"].'';
        $winningCountInside2["number"] = $lastOpenPrizeInside["number"][0].'';
        $winning["winningCount"][] =$winningCountInside1;
        $winning["winningCount"][] = $winningCountInside2;

        foreach ($winningInside as $item => $value){
            $winningInside[$item]["username"] = Utils::yc_phone($value["username"]);
        }
        $LOTTERY_BEGINS_TIME = ConfigModel::getConfigSingleValName($conn, Config::LOTTERY_BEGINS_TIME);
        $LOTTERY_OVER_TIME = ConfigModel::getConfigSingleValName($conn, Config::LOTTERY_OVER_TIME);
        $winning["winningList"] = $winningInside;
        $winning["prizeRule"][] = PrizeRuleEnum::ONE[$assemble->language];
        $winning["prizeRule"][] = PrizeRuleEnum::TWO[$assemble->language];
        $winning["prizeRule"][] = PrizeRuleEnum::THREE[$assemble->language].$LOTTERY_BEGINS_TIME."-".$LOTTERY_OVER_TIME;
        Result::success($winning);
    }
    /**
     * TODO 抽奖记录
     * entry $data number sn
     * return json
     * */
    public function winningFunds($data){
        $assemble = Common::assemble(new WinningExample(), $data);
        $conn = new Conn();
        $UserExample = Common::assemble(new UserExample(), UsersModel::getUserInside($conn, $assemble->token));
        $assemble->uid = $UserExample->id;
        $winningFunds = WinningModel::getWinningFunds($conn, $assemble);
        Result::success($winningFunds);
    }
    /**
     * TODO 参与抽奖
     * entry $data number sn
     * return json
     * */
    public function raffle($data){
        $assemble = Common::assemble(new WinningExample(), $data);
        $conn = new Conn();
        if(strlen($assemble->number) != DigitEnum::THREE){
            Result::error(ErrorEnum::WRONGLOTTERYNUMBER[$assemble->language]);
        }
        $OpenPrizeExample = Common::assemble(new OpenPrizeExample(), OpenPrizeModel::getOpenPrizeInsideInSn($conn, $assemble->sn));
        if(empty($OpenPrizeExample) || $OpenPrizeExample->state != DigitEnum::ZERO){
            Result::error(ErrorEnum::THECURRENTLOTTERYHASENDED[$assemble->language]);
        }
        $LOTTERY_BEGINS_TIME = ConfigModel::getConfigSingleValName($conn, Config::LOTTERY_BEGINS_TIME);
        $LOTTERY_OVER_TIME = ConfigModel::getConfigSingleValName($conn, Config::LOTTERY_OVER_TIME);
        $time = strtotime(date("H:i", time()));
        $LOTTERY_BEGINS_TIME = strtotime($LOTTERY_BEGINS_TIME);
        $LOTTERY_OVER_TIME = strtotime($LOTTERY_OVER_TIME);
        if($time < $LOTTERY_BEGINS_TIME || $time > $LOTTERY_OVER_TIME){
            Result::error(ErrorEnum::NONLOTTERYTIME[$assemble->language]);
        }
        $UserExample = Common::assemble(new UserExample(), UsersModel::getUserInside($conn, $assemble->token));
        if($UserExample->level <= DigitEnum::ZERO || empty($UserExample->token)){
            Result::error(ErrorEnum::NOAVAILABLELOTTERYTICKETS[$assemble->language]);
        }
        $assemble->uid = $UserExample->id;
        $assemble->username = $UserExample->username;
        $conn->shiwuStart();
        $editUserLevelInside = UsersModel::editUserLevelInside($conn, $UserExample);
        $editOpenPrizeBuyNum = OpenPrizeModel::editOpenPrizeBuyNum($conn,$OpenPrizeExample);
        $insetWinning = WinningModel::insetWinning($conn, $assemble);
        if($editUserLevelInside != DigitEnum::ONE || $editOpenPrizeBuyNum != DigitEnum::ONE || $insetWinning != DigitEnum::ONE){
            $conn->shiwuRollback();
            Result::error(ErrorEnum::PARTICIPATIONFAILED[$assemble->language]);
        }
        $conn->shiwuEnd();
        Result::success(ErrorEnum::SUCCESSFULLYPARTICIPATED[$assemble->language]);
    }
}